package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.bean.*
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class OrderDetailViewModel : BaseViewModel() {
    fun orderDetail(id:String,onResult:(OrderDetailBean)->Unit){
        viewModelScope.launch {
            val bean = mApi.orderDetail(id)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //冰箱数据
    fun iceboxData(onResult:(MutableList<IceboxBean>)->Unit,onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.iceboxData()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //冰箱的层数
    fun iceboxNumberData(id:String,onResult:(MutableList<IceBoxNumberBean>)->Unit,onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.iceboxNumberData(id)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //抽屉
    fun iceboxDrawer(id:String, onResult:(MutableList<IceboxDrawerBean>)->Unit, onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.iceboxDrawer(id)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //位置
    fun iceboxLocation(id:String, onResult:(MutableList<IceboxLocationBean>)->Unit, onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.iceboxLocation(id)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }


    //项目
    fun projects(onResult:(MutableList<ProjectBean>)->Unit, onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.projects()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //保存单个位置
    fun saveSingleLocation(sampleBoxStruId:String,
                           transportRfidSampleBoxId:String,
                           transportRfidSampleBoxName:String,
                           onResult:()->Unit, onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.saveSingleLocation(sampleBoxStruId,transportRfidSampleBoxId,transportRfidSampleBoxName)
            when(bean){
                is ApiResult.Success -> onResult()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //保存单个位置
    fun saveMultiLocation(sampleBoxStruId:String,
                          transportOrderId:String,
                          transportSampleBoxId:String, onResult:()->Unit, onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.saveMultiLocation(sampleBoxStruId,transportOrderId,transportSampleBoxId)
            when(bean){
                is ApiResult.Success -> onResult()
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //确认存入冰箱
    fun saveIcebox(orderId:String,
                   projectId:String, onResult:()->Unit, onError:(String) -> Unit){
        viewModelScope.launch {
            val bean = mApi.saveIcebox(orderId,projectId)
            when(bean){
                is ApiResult.Success -> onResult()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //盘点
    fun check(boxUID: String, onResult: (BoxSubmitBean) -> Unit, onError: (String) -> Unit) {
        viewModelScope.launch {
            val bean = mApi.submit(boxUID)
            when(bean){
                is ApiResult.Success ->onResult(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}
